package com.imu.purse.admin.controller.business;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import com.imu.common.validator.ValidatorUtils;
import com.imu.common.validator.group.AddGroup;
import com.imu.common.validator.group.UpdateGroup;
import com.imu.common.annotation.SysLog;
import com.imu.purse.modules.app.entity.CoreEntity;
import com.imu.purse.modules.app.service.CoreService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.imu.common.utils.R;

/**
 * 
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-04-16 16:15:31
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@RestController
@RequestMapping("core")
public class CoreController extends com.imu.purse.modules.sys.controller.AbstractController
{
	/****
	 * 查询
	 * @param params
 	 * @date 2018-04-16 16:15:31
	 * @return
	 */
	@SysLog("查询")
	@GetMapping("/list")
	@RequiresPermissions("core:list")
	public R list(@RequestParam Map<String, Object> params)
	{
		R r = R.ok();
		if(CollectionUtils.isEmpty(params)) return r;
		
		CoreEntity dto = BeanUtil.mapToBean(params, CoreEntity.class, true);
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
			try {
				if(null != dto.getEtime()){
					String d = format.format(dto.getEtime());  
				    Date date;
				    date = format.parse(d);
				    dto.setTimeCreate(date);
				}
				if(null != dto.getStime()){
					String d = format.format(dto.getStime());  
				    Date date;
				    date = format.parse(d);
				    dto.setTimeUpdate(date);
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		List<CoreEntity> dtos = service.queryList(dto);
		if(CollectionUtils.isEmpty(dtos)) return r;
		
		return r.put("page", new PageInfo<CoreEntity>(dtos));
	}
	
	/****
	 * 根据ID查询
	 * @param id
 	 * @date 2018-04-16 16:15:31
	 * @return
	 */
	@SysLog("根据ID查询")
	@GetMapping("/info/{id}")
	@RequiresPermissions("core:info")
	public R info(@PathVariable("id") Integer id)
	{
		return R.ok().put("dto", service.findById(id));
	}
	
	/****
	 * 新增
	 * @param dto
 	 * @date 2018-04-16 16:15:31
	 * @return
	 */
	@SysLog("新增")
	@PostMapping("/save")
	@RequiresPermissions("core:save")
	public R save(@RequestBody CoreEntity dto)
	{
		ValidatorUtils.validateEntity(dto, AddGroup.class);
		//dto.setUserCreate(this.getUserId());
		//dto.setUserUpdate(this.getUserId());
		dto.setUserCreate(this.getUserId());
		service.save(dto);
		return R.ok();
	}
	
	/****
	 * 修改
	 * @param dto
 	 * @date 2018-04-16 16:15:31
	 * @return
	 */
	@SysLog("修改")
	@PostMapping("/update")
	@RequiresPermissions("core:update")
	public R update(@RequestBody CoreEntity dto)
	{
		ValidatorUtils.validateEntity(dto, UpdateGroup.class);
		//dto.setUserUpdate(this.getUserId());
		service.update(dto);
		return R.ok();
	}
	
	/****
	 * 删除
	 * @param ids
 	 * @date 2018-04-16 16:15:31
	 * @return
	 */
	@SysLog("删除")
	@PutMapping("/delete")
	@RequiresPermissions("core:delete")
	public R delete(@RequestBody String[] ids)
	{
		service.delete(ids);
		return R.ok();
	}
	
	@Autowired
	private CoreService service;
}
